﻿using Ninject;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Web;
using System.Web.Mvc;
using Moq;
using SportsStore.Domain.Abstract;
using SportsStore.Domain.Concrete;
using SportsStore.Domain.Entities;
using System.Configuration;
using SportsStore.WebUI.Infrastructure.Abstract;
using SportsStore.WebUI.Infrastructure.Concrete;

namespace SportsStore.WebUI.Infrastructure
{
    public class NinjectDependencyResolver:IDependencyResolver
    {
        private IKernel kernel;

        public  NinjectDependencyResolver(IKernel kernelParam)
        {
            kernel = kernelParam;
            AddBindings();
        }

        public object GetService(Type serviceType)
        {
            return kernel.TryGet(serviceType);
        }

        public  IEnumerable<object> GetServices(Type serviceType)
        {
            return kernel.GetAll(serviceType);
        }


        /*
        private void AddBindings()
        {
            //将绑定放这儿
            //这是模拟数据    by 逐浪cms发哥 2020年09月11日,07:50
            Mock<IProductsRepository> mock = new Mock<IProductsRepository>();
            mock.Setup(m => m.Products).Returns(new List<Product>{
                new Product {Name = "Football",Price=25 },
                new Product {Name = "Surf board",Price=179 },
                new Product {Name = "Running shoes",Price=95 }
            });
            kernel.Bind<IProductsRepository>().ToConstant(mock.Object);
        }
        */
        
        //下面开始读EF数据 by 逐浪cms发哥 2020年09月11日,07:50
        //告诉Ninject创建的EFProductRepository类的实例，对IProductsRepository的接口进行服务，从数据读数据
        private void AddBindings()
        {
            kernel.Bind<IProductsRepository>().To<EFProductRepository>();

              EmailSettings emailSettings = new EmailSettings {
               WriteAsFile = bool.Parse(ConfigurationManager.AppSettings["Email.WriteAsFile"] ?? "false")
            };
            kernel.Bind<IOrderProcessor>().To<EmailOrderProcessor>().WithConstructorArgument("settings", emailSettings);

            //注册认证后台管理权限
            kernel.Bind<IAuthProvider>().To<FormsAuthProvider>();
        }

    }
}